home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FH-3DTUT.ZIP / 3DBASICS.TXT next >
Text File  |  1995-08-23  |  7KB  |  180 lines

  1. Basic Introduction to 3D Programming
  2. By Synergist
  3.  
  4. Ok, this is NOT fun. Not at all and I'm sure any and all text files on 3D
  5. programming have done nothing but lost you. That's why I'm here!
  6.  
  7. Ok, because today's programmer is growing younger and younger, there are a
  8. large number of you who have not taken Trigonomitry yet. Niether have I!
  9. Hell, I don't even know how to fuckin spell it. Anyways, first thing we need
  10. is a set of points. To do this, we define them! We can do this with an array. 
  11. If you are working in C++, this would be a good idea for a 12 point array:
  12.  
  13. unsigned char vector_a[36]={45, 36, 34, blah blah blah... =) };
  14.         
  15. The above defined is basically the coordinates for a 12 point vector, basic-
  16. ally a shape. =) Here's a trick part. Each point stars every three numbers.
  17. That's right. Ok don't get lost on me. I'll explain:
  18.  
  19. Say you define a single point like so:
  20. (this is pseudo code, meaning its not a language but structured like one)
  21.  
  22. define_array point_1[3] = 10, 10, 10
  23.      |          |    |        |__ coordinates on the X, Y, Z plane (SEE BELOW)
  24.      |          |    |__Number of numbers (this one has 3, see?)     
  25.      |          |__Name of the array (we call it point_1 cause its just a point)
  26.      |__Command to define an array (for whatever language you are doin this)
  27.  
  28. Ok, you defined an array. Now, what are those 10,10,10's? They are coordinates
  29.  
  30. Imagine you have a make believe plane. it goes left, right, up, down, AND in and
  31. out. 
  32.           |                   
  33.           |      /
  34.           |    /              I hope this makes sense. Anyways, say the coord 
  35.           |  /  o             inates are x=10, y=0 and z=10 the "o" shows 
  36. x_________|/____________      where that is. dont let it fool you, if you look
  37.          /|                   at it from another angle youll see its level with
  38.        /  |                   the point of origin (where all the lines cross
  39.      /    |                   or in coordinate talk, 0,0,0)
  40.    /      |
  41.  z        y
  42.    
  43.    
  44. Now, the trickey part. We have an X, Y and Z, but how do we make it so we just
  45. need X and Y? There's no Z command in putpixel or pset! help! 
  46.  
  47. Heres the trig (UGH)...
  48.  
  49. Yt = Y * COS(Xan) - Z * SIN(Xan)
  50. Zt = Y * SIN(Xan) + Z * COS(Xan)
  51. Y = Yt
  52. Z = Zt
  53. Xt = X * COS(Yan) - Z * SIN(Xan)
  54. Zt = X * SIN(Yan) + Z * COS(Xan)
  55. X = Xt
  56. Z = Zt
  57. Xt = X * COS(Zan) - Y * SIN(Zan)
  58. Yt = X * SIN(Zan) + Y * COS(Zan)
  59. X = Xt       <--we plot these!
  60. Y = Yt   <--we plot these!
  61.  
  62. See? It's magic! Ok, we take our defined array, and plug it in like so:
  63. X = point_1[1]  <--this takes the 1st number of our array and makes it X!
  64. Y = point_2[2]  <--this takes the 2nd number of our array and makes it Y!
  65. Z = point_3[3]  <--this takes the 3rd number of our array and makes it Z!
  66.  
  67. NOW we have to tell what those Xan, Yans and Zans are! Those are basically
  68. how fast you want it to rotate. Xan and Yan MUST be the same, or your vector
  69. will get all stretchy and icky and gross! Zan on the other hand can do all 
  70. sorts of neat stuff! 
  71.  
  72. The next thing we do is define the Xan, Yan and Zan:
  73.  
  74. Zan =  .3
  75. Yan =  .1
  76. Xan =  .1
  77.     
  78. Now that everything is defined, we put it all together and make a loop!
  79.  
  80. X = point_1[1]  <--this takes the 1st number of our array and makes it X!
  81. Y = point_2[2]  <--this takes the 2nd number of our array and makes it Y!
  82. Z = point_3[3]  <--this takes the 3rd number of our array and makes it Z!
  83.  
  84. Zan =  .3
  85. Yan =  .1
  86. Xan =  .1
  87.  
  88. Loop Starts here!
  89.  
  90. Yt = Y * COS(Xan) - Z * SIN(Xan)
  91. Zt = Y * SIN(Xan) + Z * COS(Xan)
  92. Y = Yt
  93. Z = Zt
  94. Xt = X * COS(Yan) - Z * SIN(Xan)
  95. Zt = X * SIN(Yan) + Z * COS(Xan)
  96. X = Xt
  97. Z = Zt
  98. Xt = X * COS(Zan) - Y * SIN(Zan)
  99. Yt = X * SIN(Zan) + Y * COS(Zan)
  100. X = Xt       <--we plot these!
  101. Y = Yt   <--we plot these!
  102.  
  103. PUTPIXEL(X, Y) <-- whatever command you want to use to plot it!
  104.  
  105. Loop ends here!
  106.  
  107. That is the basic procedure for it all. It's fastest to make a whole vector 
  108. just one array (well matrices are fastest really, but these are just the 
  109. basics) so heres how we do it (these are real coordinates for a cube):
  110.  
  111.                         x   y   z
  112.  
  113. define_array cube[24] = 40, 40, 40, 
  114.                        -40, 40, 40,
  115.                        -40,-40, 40,
  116.                         40,-40, 40,
  117.                         40, 40,-40, 
  118.                        -40, 40,-40,
  119.                        -40,-40,-40,
  120.                         40,-40,-40 
  121.  
  122. Ok. I hope you understand the above. Basically the every three numbers is a 
  123. plane coordinate. therefore, we can easily put this in our look so that
  124. it calculates all the points and plots them. Heres how we do it:
  125.  
  126. increment = -2   <-- this here is important! make sure it stars at -2!
  127.  
  128. Loop Starts here!  <--MAKE SURE IT ENDS AFTER ALL  POINTS ARE CALCULATED!!!!
  129.  
  130.  
  131. increment = increment + 3   <-- here we add 3 to the increment so it starts at
  132.                                the next x every time the loop starts again 
  133.  
  134. Yt = cube[increment+1] * COS(Xan) - cube[increment+2] * SIN(Xan) 
  135.                         
  136.                         *NOTE I added some lines in between this to explain it
  137.                         Ok, What we did was replace the Y and Z with the array
  138.                         coordinates of our current point. 
  139.                         The original, Yt = Y * COS(Xan) - Z * SIN(Xan), simply 
  140.                         gets the Y and Z replaced by array points.
  141.  
  142. Zt = cube[increment+1] * SIN(Xan) + [increment+2] * COS(Xan)
  143.                         *Same as above!!!
  144.  
  145. cube[inc+1] = Yt      *Update the points, just like above, 
  146. cube[inc+2] = Zt      *Only we update the array points!
  147.  
  148.  
  149. Xt = cube[increment] * COS(Yan) - cube[increment+2] * SIN(Xan)
  150. Zt = cube[increment] * SIN(Yan) + cube[increment+2] * COS(Xan)
  151. cube[increment] = Xt
  152. cube[increment+2] = Zt
  153.  
  154. Xt = cube[increment] * COS(Zan) - cube[increment+1] * SIN(Zan)
  155. Yt = cube[increment] * SIN(Zan) + cube[increment+1] * COS(Zan)
  156. cube[increment] = Xt       <--we plot these!
  157. cube[increment+1] = Yt   <--we plot these!
  158. Loop ends here!
  159.  
  160. NOTE! You can't do ANYTHING to these points within the loop! it will muck them
  161. up!
  162.  
  163. Ok, here we didn't plot the points. YET! After we finish caluclating all the
  164. points, we plot them. You can do this any way you want. Just take the x and
  165. y of each point (ignore the z, its only for calculations!) and plot it some
  166. place!
  167.  
  168. All done, good luck and happy coding!
  169.  
  170. FINAL NOTE: This is NOT a good file to follow if you want to make a Future Crew
  171. demo. The above code is SIMPLY a guide to start out in the 3d programming world.
  172. There are many things to do like optimization, page swapping, and matrices that
  173. i didn't go over cause those are down the road for both you AND me.
  174.  
  175. If you have any probs, drop me a line at rhr0982@grace.rit.edu!
  176.  
  177.                 -SynErgist
  178.  
  179.  
  180.